﻿$(function(){
    //jquery-ui tabs 
    $("#nav").tabs();
    //click_event
    $(".nav_li span").click(function(){
        $.when($.wait()).done(function(){$.init();}).fail(function(){alert("fail")});
    });
    //layout scroller
    $(function() {
        var scrollPane = $( "#layout" ), scrollContent = $( ".scroll-content" );
        var scrollbar = $( ".scroll-bar").slider({
            slide: function( event, ui ) {
                if ( scrollContent.height() > scrollPane.height() ) {
                    scrollContent.css( "margin-top", Math.round(
                            (1 - ui.value / 100) * ( scrollPane.height() - scrollContent.height() )
                    ) + "px" );
                } else {
                    scrollContent.css( "margin-top", 0 );
                }
            },
            orientation: "vertical",
            value: 100,
            stop: function(event, ui) {
                $(".tis").remove();
                var selected = '.blog_bg:top(' + ((1- ui.value / 100) * scrollPane.height()) + ')';
                $("#layout").append( "<span class='tis'><div>co</div></span>" );
                $(".tis").css("top", ((1 - ui.value/100) * scrollPane.height()) + "px" );
                $(".tis").click(function(){
                    $.ajax({
                        type:"POST",
                        url:"/commit/commitOut",
                        dataType:"html",
                        data:{blogId:$(selected).attr("blogId")},
                        success: function(html){
                            $("#nav").tabs("select", Number($("#commit").attr("tab")));
                            $("#commit_sh").after(html).remove();
                        },
                        complete: function(){
                            $.init();
                            $(".tis").remove();
                        }
                    });
                        
                });    
            }
        });

        var handleHelper = scrollbar.find( ".ui-slider-handle" )
        .mousedown(function() {
            scrollbar.height( handleHelper.height() );
        })
        .mouseup(function() {
            scrollbar.height( "100%" );
        })
        .append( "<span class='ui-icon ui-icon-grip-dotted-horizontal'></span>" )
        .wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

        scrollPane.css( "overflow", "hidden" );


    });
    //commit_op
    //commit_re
    $(".commit_re").live("click", function(){
                $("#commit_form_ul").remove();
                $(this).parent("p").parent("li").after(
                    function(){
                        return ('<ul id="commit_form_ul" style="margin-left:1em;"><form><textarea  name="commit" id="co_ta" rows="4"></textarea><input type="text" value="someone" id="co_te" /><input type="button" id="co_su" value="sub"  parentId=' + $(this).attr("commitId") + ' blogId=' + $(this).attr("blogId") + ' /></form></ul>');
                    }
                );
                $("#co_su").button();    
                $("#co_ta").focus();
                $.init();
                $("#co_su").click(
                    function(){
                        var from    = $.trim($("#co_te").attr("value"));
                        var content = $.trim($("#co_ta").attr("value"));
                        from = ("Ghold" == from ||  "" == from )?"someone":from;
                        if ("" == content) return;
                        $.ajax({
                            type:"POST",
                            url:"/commit/commitIn",
                            dataType:"json",
                            data:{parentId:$(this).attr("parentId"),
                                  blogId:$(this).attr("blogId"),
                                  content:$("#co_ta").attr("value"),
                                  from:from},
                            success: function(json){
                                $("#commit_form_ul").after(
                                    function(){
                                        return ('<ul class="commit_ul"><li commitId=' + json.id + ' blogId=' + $("#co_su").attr("blogId") + '><p>' + $("#co_ta").attr("value") + '</p><p><span>from ' + from + '</span><a class="commit_re" herf="#">re</a><p></li>');
                                    }
                                ).remove();
                            },
                            complete: function(){
                                $.init();
                            }
                        });
                    }
                );
            }
    );


    //footer
    $("footer").hover(
            function(){
                $("footer span").show(1000);
            },
            function(){
                $("footer span").hide();
                }
    );

    $.init();
});
//extend function
$.extend({
    //height_init
    init : function() {
        if ($("#sidebar").height() < $(window).height()) {
            $("#layout").css({'height': ($(window).height())});
        } else {
            $("#layout").css({'height': ($("#sidebar").height())});
        }
    },
    //height using Deferred obj
    wait : function(dtd) {
        var dtd = $.Deferred();
        var tasks = function(){
            dtd.resolve();
        };

        setTimeout(tasks,300);
        return dtd.promise();
    }
});
$.extend($.expr[':'],{
    top: function(a,i,m) {
        if(($(a).offset().top - 10) <= m[3] && ($(a).next(a).offset().top - 10) > m[3]){return $(a); }
    }
});
